package com.vegetable.modules.service.stock.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vegetable.common.utils.Constant;
import com.vegetable.modules.entity.stock.StockCheck;
import com.vegetable.modules.entity.stock.StockFlow;
import com.vegetable.modules.entity.stock.po.StockFlowQueryPO;
import com.vegetable.modules.entity.stock.to.StockFlowDTO;
import com.vegetable.modules.mapper.stock.StockCheckMapper;
import com.vegetable.modules.mapper.stock.StockFlowMapper;
import com.vegetable.modules.service.stock.IStockCheckService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 库存表 服务实现类
 * </p>
 *
 * @author wangruijie
 * @since 2024-03-21
 */
@Slf4j
@Service
public class StockCheckServiceImpl extends ServiceImpl<StockCheckMapper, StockCheck> implements IStockCheckService {
    @Autowired
    private StockFlowMapper stockFlowMapper;
    @Override
    public IPage statisticsStockFlow2(StockFlowQueryPO po) {
        if(ObjectUtil.isNull(po.getOrderTime())){
            po.setOrderTime(DateUtil.formatDate(new Date()));
        }
        IPage iPage = new Page(po.getCurrent(), po.getSize());
        IPage<StockFlowDTO> result = baseMapper.statisticsStockFlow2(iPage, po);
        List<StockFlowDTO> pageR = result.getRecords();
        if(CollectionUtil.isNotEmpty(pageR)){
            // 需要查询的商品id的集合
            List<Long> commodityIds = pageR.stream().map(el -> el.getCommodityId()).collect(Collectors.toList());
            QueryWrapper<StockFlow> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("commodity_id", commodityIds);
            queryWrapper.apply("DATE(order_time) = DATE('"+po.getOrderTime()+"')");
            List<StockFlow> flows = stockFlowMapper.selectList(queryWrapper);
            for(StockFlowDTO  sf : pageR){
                List<StockFlow> thisFlows = flows.stream().filter(el -> (el.getCommodityId() + "-" + el.getSpecsId()).equals(sf.getCommodityId() + "-" + sf.getSpecsId())).collect(Collectors.toList());
                List<StockFlow> allOutFlow = thisFlows.stream().filter(el -> !Constant.StockFlowType.isInType(el.getOrderType())).collect(Collectors.toList());
                List<StockFlow> allInFlow = thisFlows.stream().filter(el -> Constant.StockFlowType.isInType(el.getOrderType())).collect(Collectors.toList());
                BigDecimal outNumber = allOutFlow.stream().map(StockFlow::getNum).reduce(BigDecimal.ZERO, BigDecimal::add);
                BigDecimal inNumber = allInFlow.stream().map(StockFlow::getNum).reduce(BigDecimal.ZERO, BigDecimal::add);
                sf.setInStockFlows(allInFlow);
                sf.setInNumber(inNumber);
                sf.setOutNumber(outNumber);
                sf.setOutStockFlows(allOutFlow);
            }
        }
        return result;
    }
}
